package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.mobsandgeeks.saripaar.annotation.Or;
import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Boutique;
import com.swz.dcrm.model.BoutiqueGroup;
import com.swz.dcrm.model.Car;
import com.swz.dcrm.model.CarCompare;
import com.swz.dcrm.model.CarDetail;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.model.CarModelRelativeMerits;
import com.swz.dcrm.model.CarModelTag;
import com.swz.dcrm.model.CarOrder;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Order;
import com.swz.dcrm.model.Tab;
import com.swz.dcrm.model.Insurance;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.Quotation;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.net.IDN;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;

public class CarManagementViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private MediatorLiveData<PageResponse<CarModel>> CarModel;
    private MediatorLiveData<PageResponse<CarModel>> myCollected;
    private MediatorLiveData<BaseResponse<CarModel>> collectCarModel;
    private MediatorLiveData<BaseResponse<Object>> batchDelCarState;
    private MediatorLiveData<PageResponse<Boutique>>boutiques;
    private MediatorLiveData<PageResponse<BoutiqueGroup>> boutiqueGroup;
    private MediatorLiveData<BaseResponse<CarModel>>CarModelDetail;
    private MediatorLiveData<List<Boutique>> selectedBoutique;
    private MediatorLiveData<List<Insurance>>selectedInsurance;
    private MediatorLiveData<BaseResponse<Quotation>> addQuotation;
    private MediatorLiveData<BaseResponse<List<Insurance>>> insurances;
    private MediatorLiveData<BaseResponse<String>>applyApproval;
    private MediatorLiveData<BaseResponse<List<Tab>>>carTabs;
    private MediatorLiveData<PageResponse<Car>> carsStatus0;
    private MediatorLiveData<PageResponse<Car>> carsStatus1;
    private MediatorLiveData<PageResponse<Car>> carsStatus2;
    private MediatorLiveData<BaseResponse<CarDetail>> carDetail;
    private MediatorLiveData<BaseResponse<CarDetail>> carDetailParam;
    private MediatorLiveData<BaseResponse<CarOrder>> addCarOrder;
    private MediatorLiveData<BaseResponse<Car>>carInWareHouse;
    private MediatorLiveData<String>carNameInWareHouse;
    private MediatorLiveData<BaseResponse<List<CarModel>>> compareCars;
    private MediatorLiveData<CarModel>currentNeedCompareCar;
    private MediatorLiveData<BaseResponse<CarModelRelativeMerits>>carModelRelativeMerits;
    private MediatorLiveData<Boolean>edit;
    private MediatorLiveData<BaseResponse<String>>delCarModelRelativeMerits;
    private MediatorLiveData<BaseResponse<CarModelTag>>addCarModelRelativeMerits;
    private MediatorLiveData<BaseResponse<Quotation>>quotationById;
    private MediatorLiveData<BaseResponse<CarCompare>>carCompareConfig;
    private MediatorLiveData<Clue> currentSelectedClueRecord;
    private MediatorLiveData<BaseResponse<String>>deliveryCar;
    private MediatorLiveData<PageResponse<Order>>carOrder;
    private MediatorLiveData<BaseResponse<CarOrder>> carOrderDetail;



    public CarManagementViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<PageResponse<CarModel>> getCarModel(int page,int size,String searchName) {
        CarModel=creatLiveData(CarModel);
        dcrmApi.getCarModel(page,size,searchName).enqueue(new CallBack<>(this,CarModel)) ;
        return CarModel;
    }

    public MediatorLiveData<PageResponse<CarModel>> getMyCollected(int page,int size,String carModelName){
        myCollected=creatLiveData(myCollected);
        dcrmApi.getCollectedCarModel(page,size,carModelName).enqueue(new CallBack<>(this,myCollected)) ;

        return myCollected;
    }

    public MediatorLiveData<BaseResponse<CarModel>> collectCarModel(long id) {
        collectCarModel=creatLiveData(collectCarModel);
        dcrmApi.collectCarModel(id)
                .enqueue(new CallBack<>(this,collectCarModel));
        return collectCarModel;
    }

    public MediatorLiveData<BaseResponse<Object>> batchDelCarState(ArrayList<CarModel>list) {
        batchDelCarState=creatLiveData(batchDelCarState);
        if(list!=null&&list.size()>0){
            List<Long> ids=new ArrayList<>();
            for(CarModel CarModel:list){
                ids.add(CarModel.getId());
            }

            dcrmApi.batchDelCollected(ids)
                    .enqueue(new CallBack<>(this,batchDelCarState));
        }
        return batchDelCarState;
    }

    public MediatorLiveData<BaseResponse<CarModel>> getCollectCarModel() {
        collectCarModel=creatLiveData(collectCarModel);
        return collectCarModel;
    }

    public MediatorLiveData<BaseResponse<Object>> getBatchDelCarState() {
        batchDelCarState=creatLiveData(batchDelCarState);
        return batchDelCarState;
    }

    public MediatorLiveData<PageResponse<CarModel>> getCarModel() {
        CarModel=creatLiveData(CarModel);
        return CarModel;
    }

    public MediatorLiveData<PageResponse<BoutiqueGroup>> getBoutiqueGroup() {
        boutiqueGroup=creatLiveData(boutiqueGroup);
        dcrmApi.getBoutiqueGroup(0).enqueue(new CallBack<>(this,boutiqueGroup));
        return boutiqueGroup;
    }

    public MediatorLiveData<PageResponse<Boutique>> getBoutiques(String ids,String boutiqueName) {
        boutiques=creatLiveData(boutiques);
        dcrmApi.getBoutiques(ids,boutiqueName).enqueue(new CallBack<>(this,boutiques));
        return boutiques;
    }

    public MediatorLiveData<PageResponse<Boutique>> getBoutiques() {
        boutiques=creatLiveData(boutiques);
        return boutiques;
    }

    public MediatorLiveData<BaseResponse<CarModel>> getCarModelDetail(long id) {
        CarModelDetail=creatLiveData(CarModelDetail) ;
        CarModelDetail.setValue(null);
        dcrmApi.getCarModelDetail(id).enqueue(new CallBack<>(this,CarModelDetail));
        return CarModelDetail;
    }

    public MediatorLiveData<BaseResponse<com.swz.dcrm.model.CarModel>> getCarModelDetail() {
        CarModelDetail=creatLiveData(CarModelDetail) ;
        return CarModelDetail;
    }
    public MediatorLiveData<BaseResponse<Quotation>> addQuotation(Quotation q){
        addQuotation=creatLiveData(addQuotation);
        dcrmApi.addQuotation(q).enqueue(new CallBack<>(this,addQuotation));
        return addQuotation;
    }
    public MediatorLiveData<BaseResponse<Quotation>> getAddQuotation() {
        addQuotation=creatLiveData(addQuotation);
        return addQuotation;
    }

    public MediatorLiveData<BaseResponse<List<Insurance>>> getInsurances() {
        insurances=creatLiveData(insurances);
        dcrmApi.getInsurance(0)
                .enqueue(new CallBack<>(this,insurances));
        return insurances;
    }

    public MediatorLiveData<BaseResponse<String>> applyApproval(long id,long clueId) {
        applyApproval=creatLiveData(applyApproval);
        dcrmApi.applyApproval(id,clueId).enqueue(new CallBack<>(this,applyApproval));
        return applyApproval;
    }

    public MediatorLiveData<List<Insurance>> getSelectedInsurance() {
        selectedInsurance=creatLiveData(selectedInsurance);
        return selectedInsurance;
    }

    public MediatorLiveData<List<Boutique>> getSelectedBoutique() {
        selectedBoutique=creatLiveData(selectedBoutique);
        return selectedBoutique;
    }

    public MediatorLiveData<BaseResponse<List<Tab>>> getCarTabs() {
        carTabs=creatLiveData(carTabs);
        dcrmApi.getCarTabList().enqueue(new CallBack<>(this,carTabs));
        return carTabs;
    }
    public MediatorLiveData<BaseResponse<List<Tab>>> getCarTabsResult() {
        carTabs=creatLiveData(carTabs);
        return carTabs;
    }
    public MediatorLiveData<PageResponse<Car>> getCars(int status,int page,int size,String carModelName) {
        switch (status){
            case 0:
                carsStatus0=creatLiveData(carsStatus0);
                dcrmApi.getCarList(status,carModelName,page,size).enqueue(new CallBack<>(this,carsStatus0));
                return carsStatus0;
            case 1:
                carsStatus1=creatLiveData(carsStatus1);
                dcrmApi.getCarList(status,carModelName,page,size).enqueue(new CallBack<>(this,carsStatus1));
                return carsStatus1;
            case 2:
                carsStatus2=creatLiveData(carsStatus2);
                dcrmApi.getCarList(status,carModelName,page,size).enqueue(new CallBack<>(this,carsStatus2));
                return carsStatus2;
        }
        return null;
    }

    public MediatorLiveData<BaseResponse<CarOrder>> addCarOrder(CarOrder carOrder) {
        addCarOrder=creatLiveData(addCarOrder);
        addCarOrder.setValue(null);
        dcrmApi.addCarOrder(carOrder).enqueue(new CallBack<>(this,addCarOrder));
        return addCarOrder;
    }

    public MediatorLiveData<BaseResponse<CarOrder>> getAddCarOrder() {
        addCarOrder=creatLiveData(addCarOrder);
        return addCarOrder;
    }

    public MediatorLiveData<BaseResponse<CarDetail>> getCarDetail(long id) {
        carDetail=creatLiveData(carDetail);
        dcrmApi.getCarDetail(id).enqueue(new CallBack<>(this,carDetail));
        return carDetail;
    }

    public MediatorLiveData<BaseResponse<CarDetail>>getCarDetailParam(long id){
        carDetailParam=creatLiveData(carDetailParam);
        dcrmApi.getCarDetailParam(id).enqueue(new CallBack<>(this,carDetailParam));
        return carDetailParam;
    }

    public MediatorLiveData<BaseResponse<CarDetail>> getCarDetailParam() {
        carDetailParam=creatLiveData(carDetailParam);
        return carDetailParam;
    }

    public MediatorLiveData<BaseResponse<Car>> getCarInWareHouse() {
        carInWareHouse=creatLiveData(carInWareHouse);
        return carInWareHouse;
    }

    public MediatorLiveData<BaseResponse<Car>> getCarInWareHouse(String color, long id) {
        carInWareHouse=creatLiveData(carInWareHouse);
        dcrmApi.getCarInWareHouse(color,id).enqueue(new CallBack<>(this,carInWareHouse));
        return carInWareHouse;
    }

    public MediatorLiveData<BaseResponse<List<com.swz.dcrm.model.CarModel>>> getCompareCars() {
        compareCars=creatLiveData(compareCars);
        if(compareCars.getValue()!=null){
            compareCars.setValue(null);
        }
        dcrmApi.getCompareCar(true).enqueue(new CallBack<>(this,compareCars));
        return compareCars;
    }
    public MediatorLiveData<BaseResponse<List<com.swz.dcrm.model.CarModel>>> getCompareCarsResult() {
        compareCars=creatLiveData(compareCars);

        return compareCars;
    }
    public MediatorLiveData<String> getCarNameInWareHouse() {
        carNameInWareHouse=creatLiveData(carNameInWareHouse);
        return carNameInWareHouse;
    }

    public MediatorLiveData<com.swz.dcrm.model.CarModel> getCurrentNeedCompareCar() {
        currentNeedCompareCar=creatLiveData(currentNeedCompareCar);
        return currentNeedCompareCar;
    }

    public MediatorLiveData<BaseResponse<CarModelRelativeMerits>> getCarModelRelativeMerits(long carModelId) {
        carModelRelativeMerits=creatLiveData(carModelRelativeMerits);
        if(carModelRelativeMerits.getValue()!=null){
            carModelRelativeMerits.setValue(null);
        }
        dcrmApi.getCarModelRelativeMerits(carModelId).enqueue(new CallBack<>(this,carModelRelativeMerits));
        return carModelRelativeMerits;
    }
    public MediatorLiveData<BaseResponse<CarModelRelativeMerits>>getCarModelRelativeMerits(){
        carModelRelativeMerits=creatLiveData(carModelRelativeMerits);
        return carModelRelativeMerits;
    }
    public MediatorLiveData<BaseResponse<String>> delCarModelRelativeMerits(long id) {
        delCarModelRelativeMerits=creatLiveData(delCarModelRelativeMerits);
        dcrmApi.delCarModelRelativeMerits(id).enqueue(new CallBack<>(this,delCarModelRelativeMerits));
        return delCarModelRelativeMerits;
    }

    public MediatorLiveData<BaseResponse<String>> getDelCarModelRelativeMerits() {
        delCarModelRelativeMerits=creatLiveData(delCarModelRelativeMerits);
        return delCarModelRelativeMerits;
    }

    public MediatorLiveData<BaseResponse<CarModelTag>> addCarModelRelativeMerits(long carModelId, int typeTag, String tagContext) {
        addCarModelRelativeMerits=creatLiveData(addCarModelRelativeMerits);
        dcrmApi.addCarModelRelativeMerits(carModelId,typeTag,tagContext).enqueue(new CallBack<>(this,addCarModelRelativeMerits));
        return addCarModelRelativeMerits;
    }

    public MediatorLiveData<BaseResponse<CarModelTag>> getAddCarModelRelativeMerits() {
        addCarModelRelativeMerits=creatLiveData(addCarModelRelativeMerits);
        return addCarModelRelativeMerits;
    }

    public MediatorLiveData<BaseResponse<Quotation>> getQuotationById(long id) {
        quotationById=creatLiveData(quotationById);
        dcrmApi.getQuotationById(id).enqueue(new CallBack<>(this,quotationById));
        return quotationById;
    }

    public MediatorLiveData<BaseResponse<CarCompare>> getCarCompareConfig(long carModelId) {
        carCompareConfig=creatLiveData(carCompareConfig);
        if(carCompareConfig.getValue()==null||(carCompareConfig.getValue()!=null&&carCompareConfig.getValue().getData()
                .getCarModelDetail().getId()!=carModelId)){
            dcrmApi.getCarCompareConfig(carModelId).enqueue(new CallBack<>(this,carCompareConfig));
        }
        return carCompareConfig;
    }

    public MediatorLiveData<Clue> getCurrentSelectedClueRecord() {
        currentSelectedClueRecord=creatLiveData(currentSelectedClueRecord);
        return currentSelectedClueRecord;
    }

    public MediatorLiveData<PageResponse<Order>>getCarOrders(long clueId,int page,int size){
        carOrder=creatLiveData(carOrder);
        if(carOrder.getValue()!=null){
            carOrder.setValue(null);
        }
        dcrmApi.getCarOrder(clueId,page,size).enqueue(new CallBack<>(this,carOrder));
        return carOrder;
    }
    public MediatorLiveData<BaseResponse<String>>deliveryCar(long id){
        deliveryCar=creatLiveData(deliveryCar);
        if(deliveryCar.getValue()!=null){
            deliveryCar.setValue(null);
        }
        dcrmApi.deliveryCar(id).enqueue(new CallBack<>(this,deliveryCar));
        return deliveryCar;
    }
    public MediatorLiveData<Boolean> getEdit() {
        edit=creatLiveData(edit);
        if(edit.getValue()!=null){
            edit.setValue(null);
        }
        return edit;
    }

    public MediatorLiveData<BaseResponse<CarOrder>> getCarOrderDetail(long id) {
        carOrderDetail=creatLiveData(carOrderDetail);
        if(carOrderDetail.getValue()!=null&&carOrderDetail.getValue().getData().getId()!=id){
            carOrderDetail.setValue(null);
        }
        dcrmApi.getOrderDetail(id).enqueue(new CallBack<>(this,carOrderDetail));
        return carOrderDetail;
    }
}
